﻿using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using Zhao.IServer.Management_Arena;
using Zhao.IServer.menu;
using Zhao.IServer.User;
using Zhao.IServer;
using Zhao.Repository.Base;
using Zhao.Repository.UnitOfWork;
using Zhao.Service.Management_Arena;
using Zhao.Service.User;
using Zhao.Service;
using Zhao.Common.JWT;
using Zhao.Common.AliyunUtil.Interface;
using Zhao.Common.AliyunUtil.Impl;
using Zhao.Service.Field;
using Zhao.Service.menu;
using Zhao.IServer.Field;
using Zhao.IServer.Events;
using Zhao.Service.Events;
using Zhao.IServer.Log;
using Zhao.Service.Log;
using Zhao.Arena.MiniProgram.Api.Utility.Filters;
using Zhao.Common.Helper.Zhao.Arena;
using Zhao.Arena.MiniProgram.Api.Utility.Jwt;


namespace Zhao.Arena.MiniProgram.Api.Utility.RegisterExt
{
    /// <summary>
    /// 服务注册
    /// </summary>
    public static class RegistServiceExtension
    {
        /// <summary>
        /// 注册服务层
        /// </summary>
        /// <param name="builder"></param>
        public static void RegistService(this WebApplicationBuilder builder)
        {
            #region 依赖注入
            //仓储注入
            builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
            builder.Services.AddScoped(typeof(IBaseService<>), typeof(BaseService<>));
            builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();

            //服务层注入
            builder.Services.AddScoped<IUsersServer, UsersServer>();//用户
            builder.Services.AddScoped<IFieldTypesServer, FieldTypesServer>();//场地类型
            builder.Services.AddScoped<IFieldsServer, FieldsServer>();//场地
            builder.Services.AddScoped<IVenuesServer, VenuesServer>();//球馆表
            builder.Services.AddScoped<IFacilityTimePriceServer, FacilityTimePriceServer>();//场地预约时段价格
            builder.Services.AddScoped<IAppointmentsServer, AppointmentsServer>();//场地预约
            builder.Services.AddScoped<IAdminMenuMapServer, AdminMenuMapServer>();//管理员菜单
            builder.Services.AddScoped<IRolesServer, RolesServer>();//角色
            builder.Services.AddScoped<IMenusServer, MenusServer>();//菜单
            builder.Services.AddScoped<IAdminServer, AdminServer>();//管理员
            builder.Services.AddScoped<IVenuesImageService, VenuesImageService>();//球馆详情图片
            builder.Services.AddScoped<IVenueApplicationsServer, VenueApplicationsServer>();
            builder.Services.AddScoped<IAreaServer, AreaServer>();//区域
            builder.Services.AddScoped<IActivitiesServer, ActivitiesServer>();//活动
            builder.Services.AddScoped<IActivityMembersServer, ActivityMembersServer>();//活动参与表
            builder.Services.AddScoped<IActivityFieldsServer, ActivityFieldsServer>();//活动场地表
            builder.Services.AddScoped<IOrdersServer, OrdersServer>();//订单表
            builder.Services.AddScoped<ILogsServer, LogsServer>();//日志
            builder.Services.AddScoped<INotificationsServer, NotificationsServer>();//公告
            builder.Services.AddScoped<IBillServer,BillServer>(); //商家账单明细
            //报名记录变更表
            builder.Services.AddScoped<IActivityMemberChangeOrdersServer, ActivityMemberChangeOrdersServer>();

            builder.Services.AddScoped<IOssClient, AliyunOssClient>();//阿里云OSS操作接口

            builder.Services.AddSingleton<HolidayChecker>(); //周末或节假日帮助类 注册为单例服务

            builder.Services.AddScoped<JwtService, JwtService>();
            
            // 注册结算记录服务
            builder.Services.AddScoped<ISettlementRecordService, SettlementRecordService>();

            // 注册 AreaCacheManager 将地区类型存储到内存中 避免大量的查询数据库
            builder.Services.AddScoped<AreaCacheManager>();

            #endregion
        }


        /// <summary>
        /// AddControllers 相关
        /// </summary>
        /// <param name="builder"></param>
        public static void RegistControllers(this WebApplicationBuilder builder)
        {
            builder.Services.AddControllers(option =>
            {
                option.Filters.Add<CustomAsyncExceptionFilterAttribute>(); //异常捕捉
            })
            .AddNewtonsoftJson(options =>
            {
                //配置返回JSON首字母问题以及格式 nuget引入：Microsoft.AspNetCore.Mvc.NewtonsoftJson
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                //options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();//设置JSON返回格式同model一致

                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();//设置JSON返回格式首字母小
            })
            .AddJsonOptions(options =>
            {
                //配置解决中文乱码问题
                options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
            });

        }

    }
}
